import re
import requests
from bs4 import BeautifulSoup
import json

headers = {
    'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Mobile Safari/537.36'
}
def bilibili_get_page_info(url_str):
    page = requests.get(url=url_str, headers=headers).text
    soup = BeautifulSoup(page, 'html.parser')
    page_json=soup.find(type="application/ld+json").text
    return json.loads(page_json)

def bilibili_get_page_live(url_str):
    room_id=url_str.split('?')[0].split('/')[-1]
    request=requests.get(f'https://api.live.bilibili.com/AppRoom/index?device=phone&platform=ios&scale=3&build=10000&room_id={room_id}').json()
    res={}
    res['title']=request['data']['title']
    res['@images']=[request['data']['cover']]
    return res
